<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h2>issue群组管理系统</h2>
        <p>请登录您的账户</p>
      </div>
      
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        @submit.prevent="handleLogin"
        label-position="top"
      >
        <div class="form-item-wrapper">
          <el-form-item prop="username" class="form-item">
            <el-input
              v-model="loginForm.username"
              placeholder="请输入用户名"
              size="large"
              :prefix-icon="User"
              clearable
            />
          </el-form-item>
        </div>
        
        <div class="form-item-wrapper">
          <el-form-item prop="password" class="form-item">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              size="large"
              :prefix-icon="Lock"
              show-password
              clearable
              @keyup.enter="handleLogin"
            />
          </el-form-item>
        </div>
        
        <div class="form-item-wrapper">
          <el-form-item class="form-item">
            <el-button
              type="primary"
              size="large"
              :loading="loading"
              @click="handleLogin"
              class="login-button"
            >
              {{ loading ? '登录中...' : '登录' }}
            </el-button>
          </el-form-item>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { login } from '@/api/user.js'

export default {
  name: 'Login',
  components: {
    User,
    Lock
  },
  setup() {
    const router = useRouter()
    const loginFormRef = ref()
    const loading = ref(false)
    
    const loginForm = reactive({
      username: '',
      password: ''
    })
    
    const loginRules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'change' },
        { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'change' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'change' },
        { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'change' }
      ]
    }
    
    const handleLogin = async () => {

      if (!loginFormRef.value) return
      
      try {
        const valid = await loginFormRef.value.validate()
        if (!valid) return
        
        loading.value = true
        
        // 调用真实API接口
        const response = await login({
          username: loginForm.username,
          password: loginForm.password
        })
        
        ////console.log('登录响应:', response)
        
        if (response.code === 0 && response.token) {
          // 保存token和用户信息
          localStorage.setItem('token', response.token)
          localStorage.setItem('userInfo', JSON.stringify({
            username: loginForm.username,
            loginTime: new Date().toISOString(),
            expire: response.expire,
            token: response.token
          }))
          
          ElMessage.success('登录成功！')
          router.push('/dashboard')
        } else {
          ElMessage.error(response.msg || '登录失败！')
        }
      } catch (error) {
        //console.error('Login error:', error)
        
        // 处理不同类型的错误
        if (error.response) {
          const { status, data } = error.response
          if (status === 401) {
            ElMessage.error('用户名或密码错误！')
          } else if (status === 403) {
            ElMessage.error('账户已被禁用！')
          } else {
            ElMessage.error(data?.message || '登录失败，请重试！')
          }
        } else if (error.request) {
          ElMessage.error('网络连接失败，请检查网络！')
        } else {
          ElMessage.error('登录失败，请重试！')
        }
      } finally {
        loading.value = false
      }
    }
    
    return {
      loginFormRef,
      loginForm,
      loginRules,
      loading,
      handleLogin,
      User,
      Lock
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.login-box {
  width: 100%;
  max-width: 400px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
  padding: 40px;
  animation: slideUp 0.6s ease-out;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  color: #333;
  font-size: 24px;
  font-weight: 600;
  margin-bottom: 8px;
}

.login-header p {
  color: #666;
  font-size: 14px;
}

.login-form {
  margin-top: 20px;
  width: 100%;
}

.form-item-wrapper {
  margin-bottom: 24px;
  width: 100%;
}

.form-item-wrapper:last-child {
  margin-bottom: 0;
}

.form-item {
  margin-bottom: 0 !important;
  width: 100%;
}

.login-form .el-form-item__error {
  margin-top: 6px;
  font-size: 12px;
  line-height: 1.2;
  color: #f56c6c;
  position: relative;
  display: block;
}

.login-button {
  width: 100%;
  height: 45px;
  font-size: 16px;
  font-weight: 500;
  border-radius: 6px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
}

.login-button:hover {
  background: linear-gradient(135deg, #5a6fd8 0%, #6a4190 100%);
}


:deep(.el-input__wrapper) {
  border-radius: 6px;
  box-shadow: none !important;
  border: 1px solid #dcdfe6 !important;
  height: 45px;
  background: #fff !important;
  width: 100% !important;
  min-width: 100% !important;
}

:deep(.el-input__wrapper:hover) {
  border-color: #c0c4cc !important;
  box-shadow: none !important;
}

:deep(.el-input__wrapper.is-focus) {
  border-color: #667eea !important;
  box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.1) !important;
}

:deep(.el-input__inner) {
  height: 100%;
  line-height: 45px;
  border: none !important;
  box-shadow: none !important;
  background: transparent !important;
  width: 100% !important;
}

:deep(.el-input__suffix) {
  margin-left: 8px;
}

:deep(.el-form-item__content) {
  line-height: normal;
  width: 100%;
}

:deep(.el-form-item__error) {
  position: relative !important;
  top: 0 !important;
  left: 0 !important;
  margin-top: 6px !important;
  margin-bottom: 0 !important;
  padding: 0 !important;
  font-size: 12px !important;
  line-height: 1.2 !important;
  color: #f56c6c !important;
  display: block !important;
  width: 100% !important;
}
</style>
